use std::{collections::HashMap, thread::sleep, time::Duration};

use model_graph_channel::HttpRequest;
use model_graph_peer::data_channels::message::Message;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    let mut headers = HashMap::new();
    headers.insert(
        String::from("Content-Type"),
        String::from("application/json"),
    );
    let body = serde_json::to_string(&serde_json::json!({
        "model": "chatglm3-6b",
        "messages": [
            {
                "role":"user",
                "content":"你是谁?",
            }
        ]
    }))?;

    let request: HttpRequest = HttpRequest::post(
        String::from("http://localhost:8080/v1/chat/completions"),
        headers,
        Some(body.into_bytes()),
    )?;

    let request_bytes = bincode::serialize(&request)
        .map_err(|err| anyhow::anyhow!("{}", err))?
        .to_vec();

    let request_message = Message::request(request_bytes);

    let result =
        model_graph_channel::data_channels::proxy::local_execute_http_request(request_message)
            .await;

    match result {
        Ok(result) => {
            tracing::debug!("local_execute_http_request:{:?}", result);
        }
        Err(err) => tracing::error!("local_execute_http_request fail:{:?}", err),
    }

    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
